package module{
	
	import com.greensock.TweenLite;
	
	import flash.display.Sprite;
	import flash.geom.Point;
	
	public class Map extends Sprite{
		
		public static var instance:Map = new Map();
		
		private var blockList:Vector.<MapBlock>;
		
		private const BLOCK_WIDTH:int = 197;
		private const BLOCK_HEIGHT:int = 156;
		
		private const LEAD_WIDTH:int = 15;
		private const LEAD_HEIGHT:int = 10;
		
		public var startPoint:Point;
		
		public var startPoints:Vector.<Point>;
		
		private var colIndex:int = 2;  //列下标
		private var rowIndex:int = 2;//行下标
		
		public var isOpen:Boolean;
		
		public function Map(){
			this.blockList = new Vector.<MapBlock>;
			this.startPoints = new Vector.<Point>;
			init();
		}
		
		private function init():void{
			for(var i:int = 0;i<9;i++){
				var block:MapBlock = addChild(new MapBlock("resource/pic"+(i+1)+".jpg")) as MapBlock;
				blockList.push(block);
				var dx:int = (i%3)*(BLOCK_WIDTH + LEAD_WIDTH);
				var dy:int = int(i/3)*(BLOCK_HEIGHT + LEAD_HEIGHT);
				block.scaleX = BLOCK_WIDTH/block.width;
				block.scaleY = BLOCK_HEIGHT/block.height;
//				if(i == 3)this.startPoint = new Point(dx,dy);
				if(i%3==0)this.startPoints.push(new Point(dx,dy));
				block.x = dx;
				block.y = dy;
				block.visible = false;
			}
		}
		
		public function tweenOp():void{
			if(isOpen)return;
			for(var i:int=0;i<blockList.length;i++){
				var block:MapBlock =  blockList[i];
				block.visible = true;
				
				var sX:int =  startPoints[rowIndex].x;
				var sY:int =  startPoints[rowIndex].y;
				
				
				var dX:int = colIndex*(BLOCK_WIDTH + LEAD_WIDTH);
				var dY:int = rowIndex*(BLOCK_HEIGHT + LEAD_HEIGHT);
				block.x = sX;
				block.y = sY;
				
				rowIndex --;
				if(rowIndex < 0){
					rowIndex = 2;
					colIndex --;
				}
				TweenLite.to(block,0.4,{x:dX,y:dY,scaleX:BLOCK_WIDTH/block.img.width,scaleY:BLOCK_HEIGHT/block.img.height});
				if(i==blockList.length-1)isOpen = true;
			}
		}
		
	
		public function onResize():void{
			this.x = ((Global.stage.stageWidth - width)>>1) + width/3;
			this.y = ((Global.stage.stageHeight - height)>>1) + height/5;
		}
	}
}